/*----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the 
License at http://www.mozilla.org/MPL/
	
Software distributed under the License is distributed on 
an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, 
either express or implied. 
See the License for the specific language governing rights and 
limitations under the License.
	
The Original Code is Fever RS ( ActionScript 3 ) Framework code.
	
The Initial Developer of the Original Code is Romain Ecarnot.
Portions created by Initial Developer are Copyright (C) 2006
the Initial Developer. All Rights Reserved.
	
Contributor(s):
	
Unless required by applicable law or agreed to in writing, 
software distributed under the License is distributed on 
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
OF ANY KIND, either express or implied.
See the License for the specific language governing permissions 
and limitations under the License.
----------------------------------------------------------------------------*/

package fever.plugin.analytic.strategy 
{
	import flash.events.Event;
	import flash.external.ExternalInterface;
	
	import fever.core.RSCore;
	import fever.events.AnalyticEvent;
	import fever.plugin.analytic.AnalyticTracker;
	import fever.utils.StringUtil;
	import fever.utils.Stringifier;	

	/**
	 * Track application / site internal navigation throw 
	 * <strong>Google Analytic</strong> system.
	 * 
	 * <p>Use as <code>AnalyticPlugin</code> tracking stategy</p>
	 * 
	 * @see fever.plugin.analytic.AnalyticPlugin
	 * 
	 * @author Romain Ecarnot
	 */
	public class GoogleAnalytic implements AnalyticTracker
	{
		//--------------------------------------------------------------------
		// Public properties
		//--------------------------------------------------------------------
		
		/**
		 * Google Analytic urchin method name.
		 * 
		 * @default "urchinTracker"
		 */
		public static var URCHIN_METHOD_NAME : String = "urchinTracker";
		
				
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Constructor.
		 */
		public function GoogleAnalytic()
		{
			RSCore.getLogger().warn( "GoogleAnalytic instance" );
		}
		
		/** 
		 * @inheritDoc
		 */
		public function track( event : AnalyticEvent ) : void
		{
			if( event )
			{
				var linkURL : String = event.linkURL;
				var linkType : String = event.linkType;
				
				linkURL = linkURL.replace( " ", "_" );
				linkURL = linkURL.replace( "http://", "" );
				linkURL = linkURL.replace( "https://", "" );
				linkURL = "/" + StringUtil.trim( linkURL, StringUtil.EMPTY_CHARS + "/" );
				
				if( linkType != null ) linkURL = "/" + linkType + linkURL;
				
				if( ExternalInterface.available ) 
					ExternalInterface.call( URCHIN_METHOD_NAME, linkURL );
				
				RSCore.getLogger().info( "GoogleAnalytic tracking :: " + linkURL );
			}
		}

		/**
		 * Execute the request according to the current 
		 * command data.
		 * 
		 * @param	e An event that will be used as data source 
		 * by the command
		 */
		public function execute( e : Event = null ) : void
		{
			track( e as AnalyticEvent );
		}
		
		/**
		 * Returns string representation.
		 */
		public function toString() : String
		{
			return Stringifier.parse( this );	
		}
	}
}
